package bigdata36.damin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

public class TableDataCURD1 {
	/**
	 * 测试增加数据   （修改数据）
	 * @throws IOException 
	 */
	@Test
	public void testPutData() throws IOException{
		Configuration config = HBaseConfiguration.create();
		Connection connection = ConnectionFactory.createConnection(config);
		
		Table table = connection.getTable(TableName.valueOf("t_user"));
		Put put = new Put(Bytes.toBytes("009rk"));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name_2"), Bytes.toBytes("lisi"));
		put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("18"));
		
		
		Put put2 = new Put(Bytes.toBytes("010rk"));
		put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("nam2"), Bytes.toBytes("zhangsan"));
		put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("19"));
		
		List<Put> list = new ArrayList<>();
		list.add(put);
		list.add(put2);
		table.put(list);
		
		table.close();
		connection.close();
	}
	
	/**
	 * 删除
	 * @throws Exception 
	 */
	@Test
	public void testDelete() throws Exception{
		Configuration config = HBaseConfiguration.create();
		Connection connection = ConnectionFactory.createConnection(config);
		
		Table table = connection.getTable(TableName.valueOf("t_user"));
		Delete delete = new Delete(Bytes.toBytes("rk001"));
		//  //多版本
		delete.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
		table.delete(delete);
		table.close();
		connection.close();
	}
}
